﻿@{
    Layout = "~/Views/Shared/_List.cshtml";
    var module = "/Draw/Entry";
    var moduleSize = new { width = 1000, height = 700 };
}
<div class="layui-search layui-form">
    <a id="btnAdd" class="layui-btn">新增</a><a id="btnDelist" class="layui-btn" data-type="delist">删除</a>
    <div class="layui-form-search">
        <div class="layui-inline">
            <select id="selType" lay-filter="typeChange">
                <option value="">分类</option>
                @foreach (Domain.Com.BasicType item in ViewBag.Types)
                {
                    <option value="@(item.Id)">@(item.Title)</option>
                }
            </select>
        </div>
        <div class="layui-inline">
            <select id="selType2" lay-filter="type2Change">
                <option value="">分类2</option>
            </select>
        </div>
        <div class="layui-inline">
            <select id="selType3">
                <option value="">分类3</option>
            </select>
        </div>
        <div class="layui-inline">
            <input id="txtKeywords" class="layui-input" placeholder="请输入查询条件" data-type="reload">
        </div>
        <a id="btnSearch" class="layui-btn" data-type="reload">查询</a>
    </div>
</div>
<table id="list" lay-filter="list"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

@section script{
    <script>
        var $;
        layui.use(['table', 'form'], function () {
            var table = layui.table
                , form = layui.form;
            var tableIns = table.render({
                elem: '#list'
                , id: 'list'
                , url: '@module/List'
                , where: {
                    Type: $('#selType').val(),
                    Type2: $('#selType2').val(),
                    Type3: $('#selType3').val(),
                    Keywords: $('#txtKeywords').val()
                }
                , page: true
                , limits: [10, 15, 20, 25, 30]
                , limit: 15
                , height: 'full-68'
                , cols: [[ //标题栏
                    { checkbox: true, fixed: true }
                    , { title: '分类', field: 'typeName', width: 150 }
                    , { title: '分类2', field: 'type2Name', width: 150 }
                    , { title: '分类3', field: 'type3Name', width: 150 }
                    , { title: '标题', field: 'title' }
                    , { title: '状态', field: 'statusName', width: 150 }
                    , { title: '创建时间', field: 'createTime', width: 200 }
                    , { title: '操作', width: 140, align: 'center', toolbar: '#barDemo', fixed: "right" }

                ]]
            });

            $ = layui.$;
            var active = {
                reload: function () {

                    table.reload('list', {
                        where: {
                            Type: $('#selType').val(),
                            Type2: $('#selType2').val(),
                            Type3: $('#selType3').val(),
                            Keywords: $('#txtKeywords').val()
                        }, page: {
                            curr: 1
                        }
                    });
                },
                delist: function () { //获取选中数据
                    var checkStatus = table.checkStatus('list')
                        , data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg('未选中任何数据', { icon: 0 });
                        return;
                    }
                    var ids = [];
                    $.each(data, function (index, item) {
                        ids.push(item.id);
                    })
                    layer.confirm('真的删除所选行么', function (index) {
                        $.post("@module/Delist", { ids: ids }, function (d) {
                            if (d.code === 0) {
                                layer.msg(d.msg, { icon: 1 });
                                $('#btnSearch').click();
                            }
                            else {
                                layer.msg(d.msg, { icon: 2 });
                            }
                        });
                        layer.close(index);
                    });
                }
            };

            function activeCall(obj) {
                var type = $(obj).data('type');
                active[type] ? active[type].call(obj) : '';
            }
            $('#btnSearch,#btnDelist').on('click', function () {
                activeCall(this);
            });
            $('#txtKeyword').on('keyup', function (e) {
                if (e.keyCode === 13) activeCall(this);
            });
            $('#btnAdd').click(function () {
                add('新增', '@module/Edit', '@moduleSize.width', '@moduleSize.height', true, function () { table.reloadData('list', {}); });
            })

            form.on('select(typeChange)', function (data) {
                $("#selType2").empty();
                $("#selType2").append("<option value=\"\">分类2</option>");
                $("#selType3").empty();
                $("#selType3").append("<option value=\"\">分类3</option>");
                var type = $("#selType").val();
                if (type) {
                    $.post("/Basic/BasicType/GetList", { Type: "EntryType", ParentId: type }, function (d) {
                        if (d.code === 0) {
                            d.data.forEach(function (item) {
                                $("#selType2").append("<option value=\"" + item.id + "\">" + item.title + "</option>");
                            });
                        }
                        form.render('select');
                    });
                }else{
                    form.render('select');
                }
            });

            form.on('select(type2Change)', function (data) {
                $("#selType3").empty();
                $("#selType3").append("<option value=\"\">分类3</option>");
                var type2 = $("#selType2").val();
                if (type2) {
                    $.post("/Basic/BasicType/GetList", { Type: "EntryType", ParentId: type2 }, function (d) {
                        if (d.code === 0) {
                            d.data.forEach(function (item) {
                                $("#selType3").append("<option value=\"" + item.id + "\">" + item.title + "</option>");
                            });
                        }
                        form.render('select');
                    });
                }else{
                    form.render('select');
                }
            });

            //监听工具条
            table.on('tool(list)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.post("@module/Del", { id: data.id }, function (d) {
                            if (d.code === 0) {
                                layer.msg(d.msg, { icon: 1 });
                                $('#btnSearch').click();
                            }
                            else {
                                layer.msg(d.msg, { icon: 2 });
                            }
                        });
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    add('编辑', '@module/Edit?id=' + data.id, '@moduleSize.width', '@moduleSize.height', true, function () { table.reloadData('list', {}); });
                }
            });
        });
    </script>
}


